package j2025.j03;

/**
 * @Author 猫大仙
 */
public class j0312 {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head,fast = head;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
            if(fast==slow) break;
        }
        if(fast ==null || fast.next==null){
            return null;
        }
        fast = head;
        //slow在fast一半,再走一半走到环起点
        while(fast!=slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

